package com.ssy.bus.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ssy.bus.mapper.CaseMapper;
import com.ssy.bus.mapper.CaseOrderMapper;
import com.ssy.bus.mapper.DrugInfoMapper;
import com.ssy.bus.mapper.PrescriptionMapper;
import com.ssy.bus.pojo.Case;
import com.ssy.bus.pojo.CaseOrder;
import com.ssy.bus.pojo.Prescription;
import com.ssy.bus.service.ICaseOrderService;
import com.ssy.bus.vo.CaseOrderVo;
import com.ssy.common.CodeMsg;
import com.ssy.common.utils.PageResult;
import com.ssy.common.utils.Result;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author ssy
 */
@Service
public class CaseOrderServiceImpl extends ServiceImpl<CaseOrderMapper, CaseOrder> implements ICaseOrderService {

    @Resource
    private CaseOrderMapper caseOrderMapper;

    @Resource
    private CaseMapper caseMapper;

    @Resource
    private DrugInfoMapper drugInfoMapper;

    @Resource
    private PrescriptionMapper prescriptionMapper;

    @Override
    public Result insertCaseOrder(CaseOrder caseOrder) {
        if (caseOrder == null) {
            return new Result(CodeMsg.PAY_ERROR);
        }

        CaseOrder caseOrder1 = this.caseOrderMapper.selectOne(new LambdaQueryWrapper<CaseOrder>().eq(CaseOrder::getOutpatientId, caseOrder.getOutpatientId()));
        if (caseOrder1 != null && caseOrder1.getOrderState() == 1) {
            return new Result(CodeMsg.PAY_REPEAT_ERROR);
        }
        prescriptionMapper.updatePrescriptionPayState(caseOrder.getOutpatientId());
        int i = this.caseOrderMapper.insert(caseOrder);
        return i > 0 ? new Result(CodeMsg.PAY_SUCCESS) : new Result(CodeMsg.PAY_ERROR);
    }

    @Override
    public PageResult selectCaseOrderByPage(CaseOrderVo caseOrderVo) {
        Page<CaseOrder> page = new Page<>(caseOrderVo.getPage(), caseOrderVo.getLimit());
        IPage<CaseOrder> caseOrderIPage = this.caseOrderMapper.selectPage(page, new LambdaQueryWrapper<CaseOrder>()
                .eq(StringUtils.hasText(caseOrderVo.getOutpatientId()), CaseOrder::getOutpatientId, caseOrderVo.getOutpatientId())
                .like(StringUtils.hasText(caseOrderVo.getPatientName()), CaseOrder::getPatientName, caseOrderVo.getPatientName())
                .orderByDesc(CaseOrder::getCreateTime));
        return new PageResult(caseOrderIPage.getTotal(), caseOrderIPage.getRecords());
    }

    @Override
    public Result grantDrug(String outpatientId) {
        if (outpatientId == null) {
            return new Result(CodeMsg.DRANT_DRUG_ERROR);
        }

        Case aCase = this.caseMapper.selectOne(new LambdaQueryWrapper<Case>()
                .eq(Case::getOutpatientId, outpatientId));
        if (aCase == null) {
            return new Result(CodeMsg.PLEASE_ENTER_ERROR);
        } else if (aCase.getIsGrantDrug() == 1) {
            return new Result(CodeMsg.DRANT_DRUG_TWO_ERROR);
        }

        List<Prescription> prescriptions = this.prescriptionMapper.selectList(new LambdaQueryWrapper<Prescription>()
                .eq(Prescription::getOutpatientId, outpatientId)
                .eq(Prescription::getType, 1));

        for (Prescription prescription : prescriptions) {
            //获取药品ID
            Integer drugId = prescription.getPreId();
            Integer number = prescription.getNumber();
            this.drugInfoMapper.updateDrugInfoNumberTwo(drugId, number);
        }
        Case aCase1 = new Case();
        aCase1.setIsGrantDrug(1);
        this.caseMapper.update(aCase1, new LambdaQueryWrapper<Case>().eq(Case::getOutpatientId, outpatientId));
        return new Result(CodeMsg.DRANT_DRUG_SUCCESS);
    }
}
